<?php
	class Data
	{
		private static $encrypt_key = '38somoaN46549IK3uMu45nZ30';				// For security, you need to change this text value to something else.	
		
		private static $encrypt_type = MCRYPT_3DES;
		private static $encrypt_mode = MCRYPT_MODE_ECB;
		private static $encrypt_iv_size;
		private static $encrypt_iv;

		#
		# Public
		#
		
		public static function encrypt( $text )
		{
			self::set_values();
			return self::base64url_encode( mcrypt_encrypt( self::$encrypt_type, ENCRYPT_KEY, $text,self::$encrypt_mode, self::$encrypt_iv ) );
		}
		
		public static function decrypt( $ciphertext)
		{
			self::set_values();
			return rtrim( mcrypt_decrypt( self::$encrypt_type, ENCRYPT_KEY, self::base64url_decode($ciphertext), self::$encrypt_mode, self::$encrypt_iv ),"\0" );
		}
		
		public static function sanitize( $input )
		{
			if (is_array($input)) 
			{
				foreach($input as $var=>$val) 
				{
					$output[$var] = self::sanitize($val);
				}
			}
			else 
			{
				if (get_magic_quotes_gpc()) 
				{
					$input = stripslashes($input);
				}
				$input  = self::cleanInput($input);
				$output = mysql_real_escape_string($input);
			}
			return $output;
		}

		
		#
		# Private 
		#
		
		private static function cleanInput($input) 
		{
			$search = array
			(
				'@<script[^>]*?>.*?</script>@si',   // Strip out javascript
				'@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
				'@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
				'@<![\s\S]*?--[ \t\n\r]*>@'         // Strip multi-line comments
			);
		 
			$output = preg_replace($search, '', $input);
			return $output;
		}
	
		private static function set_values()
		{
			self::$encrypt_iv_size = mcrypt_get_iv_size(MCRYPT_3DES, MCRYPT_MODE_ECB);
			self::$encrypt_iv = mcrypt_create_iv(self::$encrypt_iv_size, MCRYPT_RAND); 	
		}
		
		private function base64url_encode($plainText)
		{
			$base64 = base64_encode($plainText);
			$base64url = strtr($base64, '+/=', '-_*');
			return ($base64url);   
		}
		
		private function base64url_decode($base64url)
		{
			$base64 = strtr($base64url, '-_*', '+/=');
			$plainText = base64_decode($base64);
			return ($plainText);   
		}
		
	}
?>